﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoginServer.Networking.Handlers
{
    class HANDLE_NICKNAME : PacketHandler
    {
        public override void Handle(LoginServer.Virtual_Objects.User.virtualUser User)
        {
            int UserID = DB.runRead("SELECT id FROM users WHERE username='" + DB.Stripslash(User.Username) + "'", null);
            if (UserID > 0)
            {
                string nickName = DB.Stripslash(getNextBlock());
                string[] checkUsedNick = DB.runReadRow("SELECT * FROM users WHERE nickname='" + nickName + "'");
                if (checkUsedNick.Length > 0)
                {
                    User.send(new Packets.PACKET_SERVER_LIST(Packets.PACKET_SERVER_LIST.errorCodes.AlreadyUsedNick));
                }
                else
                {
                    if (nickName.Contains(" ")) return;
                    DB.runQuery("UPDATE users SET nickname='" + nickName + "', firstlogin='2' WHERE id='" + User.UserID + "'");
                    string[] UserData = DB.runReadRow("SELECT id, username, password, salt, online, nickname, rank, firstlogin FROM users WHERE id=" + UserID.ToString());
                    User.send(new Packets.PACKET_SERVER_LIST(int.Parse(UserData[0]), UserData[1], UserData[2], UserData[5], User.SessionID, int.Parse(UserData[6])));
                }
            }
        }
    }
}
